package com.gy.education.mine;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gy.education.BaseActivity;
import com.gy.education.R;
import com.gy.education.utils.ActivityUtil;
import com.gy.education.utils.BitmapUtil;
import com.gy.education.utils.StringUtil;
import com.nostra13.universalimageloader.utils.L;

import java.io.File;
import java.io.IOException;

public class ShowActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivPic = null; // 显示图片控件
    private TextView tv_chongpai;
    private TextView tv_queding;
    private String path = "";
    private Bitmap bitmapGk;
    private static final int SUCCESS_POSTPIC_1 = 1;
    private static final int FAIL_POSTPIC = 2;
    private int position;


    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_show);
        hidenTop();
        ivPic = (ImageView) findViewById(R.id.ivPic);
        tv_chongpai = (TextView) findViewById(R.id.tv_chongpai);
        tv_queding = (TextView) findViewById(R.id.tv_queding);
    }

    @Override
    protected void initData() {
        setImageBitmap(getImageFormBundle());
        path = getIntent().getStringExtra("path");
        position = getIntent().getIntExtra("position",0);
    }

    @Override
    protected void setListener() {
        super.setListener();
        tv_chongpai.setOnClickListener(this);
        tv_queding.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_chongpai:
                finish();
                break;
            case R.id.tv_queding:
                doPostUpload();
                break;
        }
    }


    /**
     * 将MainActivity传过来的图片显示在界面当中
     *
     * @param bytes
     */
    public void setImageBitmap(byte[] bytes) {
        Bitmap cameraBitmap = byte2Bitmap();
        // 根据拍摄的方向旋转图像（纵向拍摄时要需要将图像选择90度)
        Matrix matrix = new Matrix();
        matrix.setRotate(ActivityUtil.getPreviewDegree(this));
        cameraBitmap = Bitmap
                .createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(),
                        cameraBitmap.getHeight(), matrix, true);
        bitmapGk = cameraBitmap;
        ivPic.setImageBitmap(cameraBitmap);
    }

    /**
     * 从Bundle对象中获取数据
     *
     * @return
     */
    public byte[] getImageFormBundle() {
        Intent intent = getIntent();
        Bundle data = intent.getExtras();
        byte[] bytes = data.getByteArray("bytes");
        return bytes;
    }

    /**
     * 将字节数组的图形数据转换为Bitmap
     *
     * @return
     */
    private Bitmap byte2Bitmap() {
        byte[] data = getImageFormBundle();
        // 将byte数组转换成Bitmap对象
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        return bitmap;
    }

    private void doPostUpload() {
        showLoadDialog();
        File file = new File(path);
        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inTempStorage = new byte[1024 * 100];
        options.inSampleSize = 4;
        int bitmapDegree = StringUtil.getBitmapDegree(file.getAbsolutePath());
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        Bitmap bitmap1 = StringUtil.rotateBitmapByDegree(bitmap, bitmapDegree);

        try {
            BitmapUtil.saveBitmap(bitmap1, file.getParent());
        } catch (IOException e) {
            e.printStackTrace();
        }
        File file1 = new File(file.getParent() + "/image.png");
        if (!file1.exists()) {
            L.i(file1.getPath() + "...");
            file1 = new File(path);
        }

    }

}